# 创建数据库引擎
from contextlib import contextmanager
from sqlalchemy import create_engine
from sqlalchemy.orm import Session

SQLALCHEMY_DATABASE_URI = f"mysql+pymysql://root:123456@192.168.1.160:13306/app?charset=utf8mb4"
    
# 获取数据库引擎选项

engine = create_engine(SQLALCHEMY_DATABASE_URI, echo=True)



def get_session():
    with Session(engine) as session:
        yield session


@contextmanager
def transactional_session(db: Session):
    """事务性会话上下文管理器"""
    try:
        yield db
        db.commit()
    except Exception as e:
        db.rollback()
        raise e